package com.light.mapcomponents;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import com.badlogic.gdx.math.Vector2;
import com.light.gamecomponents.CullingSprite;
import com.light.gamecomponents.IGameComponents;
import com.light.roller.Player;
import com.light.roller.ResourceManager;
import com.light.roller.Roller;
import com.light.roller.Roller_2;
import com.light.roller.Stage;

public class DoorComponents implements IGameComponents, IActionComponents{

	protected Roller m_game;
	protected TextureRegion m_textureRegion;
	protected CullingSprite m_sprite;
	protected String m_resourceName;
	protected Vector2 m_initialPosition;
	protected float m_initialRotation;
	
	public DoorComponents(Roller game, String resourceName, Vector2 initialPosition, float initialRotation) {
		m_game = game;
		m_resourceName = resourceName;
		m_initialPosition = initialPosition;
		m_initialRotation = initialRotation;
	}
	
	@Override
	public void onLoadEngine() {
		
	}

	@Override
	public void onLoadResources() {
		ResourceManager.onLoadResources(m_resourceName, m_game);
		this.m_textureRegion = ResourceManager.getTextureRegion(m_resourceName);
	}

	@Override
	public void unLoadResources() {
		this.m_textureRegion = null;
		ResourceManager.unLoadResources(m_resourceName, m_game);
		
	}

	@Override
	public void onLoadScene(Scene scene, PhysicsWorld physicsWrold) {
		m_sprite = new CullingSprite(m_initialPosition.x,
				              		 m_initialPosition.y,
				              		 m_textureRegion);
		
		m_sprite.setRotation(m_initialRotation);
		
		scene.attachChild(m_sprite);
	}

	@Override
	public void removeFromScene(Scene scene, PhysicsWorld physicsWorld) {
		scene.detachChild(m_sprite);		
	}

	@Override
	public void action(Player player, Stage stage) {
		m_game.preSwitchToNextStage();
		//m_game.stageSwitch(Roller_2.class);
	}

	@Override
	public boolean collided(Player player) {
		if (player.getSprite().collidesWith(m_sprite))
			return true;
		else
			return false;
	}

	@Override
	public void rotate(float angle) {
		m_sprite.setRotation(angle);
	}

}
